applescript : pages to txt Hint

set parmStat to true
repeat
	set tmpNUM to display dialog "시작 페이지 번호?" default answer "" with icon note buttons {"Cancel", "Continue"} default button "Continue"
	--> {button returned:"Continue", text returned:"3"}
	--display dialog "Hello, " & (text returned of startPG) & "."
	try
		set startPG to (text returned of tmpNUM) as number
		if startPG is greater than 0 then
			exit repeat
		else
			set startPG to 3
			display dialog "3페이지부터 시작합니다." buttons {"확인"} default button 1
		end if
		exit repeat
		
		
	on error
		set parmStat to false
		display dialog "숫자를 입력하세요." buttons {"종료"} default button 1
		exit repeat
	end try
	
end repeat
set startPG to (startPG - 1)
if parmStat is false then return

tell application "Finder"
	activate
	set inFile to choose file with prompt "Please select a document to process:"
	set pageFile to inFile
	set inKind to kind of inFile
	set pageExt to name extension of inFile
	set pageFilePath to pageFile as text
	set pageFilePathNoExt to text 1 thru -((count pageExt) + 2) of pageFilePath
	set pageNameExt to (name of inFile)
	set pageName to text 1 thru -((count pageExt) + 2) of pageNameExt
end tell

if not inKind is "Pages 도큐멘트" then
	display alert "Invalid Pages document" giving up after 1
	return
end if

set outFilePath to pageFilePathNoExt & ".txt"
set outFilePOSIXPath to (the POSIX path of outFilePath)

set outFilePOSIXDir to (the POSIX path of pageFilePathNoExt)

do shell script "mkdir -p " & quoted form of outFilePOSIXDir


tell application "Pages"
	open pageFile
	activate
	tell front document
		if document body is false then error number -128
		
		get the count of sections
		get the count of pages of the first section
		get the count of pages of every section
		
		set pg to (0 - startPG)
		
		set the sectionCount to the count of sections
		repeat with s from 1 to sectionCount by 1
			tell section s of it
				
				set the pageCount to the count of the pages
				repeat with p from 1 to the pageCount by 1
					set pg to (pg + 1)
					tell page p of it
						log "p:" & p
						log "pg:" & pg
						log it
						
						set tmpText to body text of it
						set outFilePath to (pageFilePathNoExt)
						set outFilePOSIXPath to (the POSIX path of outFilePath) & "/" & pageName & "-PG" & pg & ".txt"
						
						set outFilePOSIXPathTTS to (the POSIX path of outFilePath) & "/" & "TTS-" & pageName & "-PG" & pg & ".txt"
						
						if tmpText is not "" then
							do shell script "echo " & quoted form of tmpText & " > " & quoted form of outFilePOSIXPath & " "
							do shell script "cat " & quoted form of outFilePOSIXPath & " | sed  's/([^(]*)/()/g' > " & quoted form of outFilePOSIXPathTTS
							
						end if
						
					end tell
					
				end repeat
			end tell
			
		end repeat
		
	end tell
end tell